﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace Tools.Helper
{
    public class BuildMapHelper
    {
        static List<BuildMap> buildMaps;
        private class BuildMap
        {
            public string Path { set; get; }
            public string[] Extension { set; get; }
            public string Out { set; get; }
        }

        static BuildMapHelper()
        {
            XmlNodeList nodelist = Config.GetConfigObject("/root/BuildMap/map");
            buildMaps = new List<BuildMap>();
            foreach (XmlNode item in nodelist)
            {
                buildMaps.Add(new BuildMap()
                {
                    Path = item.Attributes["path"].Value,
                    Extension = item.Attributes["extension"].Value.Split(';'),
                    Out = item.Attributes["out"].Value
                });
            }
        }

        public static List<string> GetBuildMapFile(string[] flist)
        {
            List<string> list = new List<string>();
            FileInfo f;
            foreach (string str in flist)
            {
                f = new FileInfo(str);
                var buildMap = (from p in buildMaps
                                where str.StartsWith(p.Path, StringComparison.OrdinalIgnoreCase)
                                && p.Extension.Any(x => string.Compare(f.Extension, x, true) == 0 || x == ".")
                                orderby p.Path.Length descending
                                select p);
                if (buildMap == null || buildMap.Count() == 0)
                    list.Add(str);
                else
                    list.Add(buildMap.First().Out);
            }

            return list.Distinct().ToList();
        }
    }
}
